<script type="text/html" template lay-done="layui.data.sendParams(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-CoreCmsLabel-editForm" id="LAY-app-CoreCmsLabel-editForm">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>已选标签</legend>
        </fieldset>
        <input type="hidden" name="ids" value="{{d.params.data.ids.id}}">
        <div class="layui-form-item" id="select_label">
            {{#  if(d.params.data.labels.length<=0){ }}
            所选商品暂无标签
            {{#  }  }}
        </div>

        <div class="layui-form-item">
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-CoreCmsLabel-editForm-submit" id="LAY-app-CoreCmsLabel-editForm-submit" value="确认编辑">
        </div>
    </div>
</script>
<script>
    var selected = new Array;
    var debug = layui.setter.debug;
    layui.data.sendParams = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['admin', 'form', 'laydate', 'upload', 'coreHelper'],
            function () {
                var $ = layui.$
                    , form = layui.form
                    , admin = layui.admin
                    , laydate = layui.laydate
                    , upload = layui.upload
                    , coreHelper = layui.coreHelper;

                var labels = d.params.data.labels;
                if (labels.length > 0) {
                    $.each(labels, function (i, j) {
                        appendLabel(j.name, j.style);
                    })
                }

                //删除标签
                $(".layui-layer-content").on('click', '.layui-unselect', function () {
                    var label = $(this).parent().parent();
                    var label_text = label.attr('data-text');
                    if (selected.length > 0) {
                        $.each(selected, function (i, j) {
                            if (selected[i]) {
                                if (j.text == label_text) {
                                    selected.splice(i, 1);
                                    label.remove();
                                }
                            }
                        });
                    }
                });
                //追加标签
                function appendLabel(label_text, label_style) {
                    var current = { 'text': label_text, 'style': label_style };
                    var isAppend = true;
                    $.each(selected, function (i, j) {
                        if (j.text == label_text) {
                            layer.msg(label_text + '标签已存在');
                            isAppend = false;
                            return false;
                        }
                    });
                    if (isAppend) {
                        selected.push(current);
                        switch (label_style) {
                            case 'red':
                                label_style = "";
                                break;
                            case 'green':
                                label_style = "layui-bg-green";
                                break;
                            case 'orange':
                                label_style = "layui-bg-orange";
                                break;
                            case 'blue':
                                label_style = "layui-bg-blue";
                                break;
                            default:
                                label_style = '';
                        }
                        var html = '<div class="layui-inline" data-text="' + label_text + '"><label class="layui-form-label"><span class="layui-badge ' + label_style + '" >' + label_text + '</span><i class="layui-icon layui-unselect layui-tab-close">ဆ</i></label></div>';
                        $("#select_label").append(html);
                    }
                }

                //重载form
                form.render(null, 'LAY-app-CoreCmsLabel-editForm');
            })
    };
    function getSelected() {
        if (selected <= 0) {
            return [];
        }
        return selected;
    }
</script>